﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    [JwtAuthentication]
    public class A1A2EmployeeFrameFeedingConfirmationController : ApiController
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post([FromBody] A1A2EmployeeFrameFeedingConfirmation FFC)
        {
            ReturnObject RO = new ReturnObject();
            if (FFC == null ||
                string.IsNullOrEmpty(FFC.A1A2EFFC_ConfirmationTiming) ||
                string.IsNullOrEmpty(FFC.A1A2EFFC_Feeder) ||
                string.IsNullOrEmpty(FFC.A1A2EFFC_ProductModel) ||
                string.IsNullOrEmpty(FFC.A1A2EFFC_LineBody))
            {
                Logger.Warn("输入值无效");
                RO.Status = HttpStatusCode.BadRequest;
                RO.Message = "A1A2EmployeeFrameFeedingConfirmation:输入值无效";
                RO.Data = null;
                return Request.CreateResponse(HttpStatusCode.BadRequest, RO);
            }

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        var query = @"INSERT INTO A1A2EmployeeFrameFeedingConfirmation 
                                    (FFCFeedingDate, FFCConfirmationTiming, FFCFeeder, 
                                     FFCFeedingQuantity, FFCProductModel, FFCLineBody, FFCPhotoUpload,FFCCreateCreator,FFCCreateTime,FFCFormType)
                                    VALUES 
                                    (@FeedingDate, @ConfirmationTiming, @Feeder, 
                                     @FeedingQuantity, @ProductModel, @LineBody, @PhotoUpload,@CreateCreator,@CreateTime,@FormType);
                                    SELECT CAST(scope_identity() AS int);";

                        SqlParameter[] parameters = {
                            new SqlParameter("@FeedingDate", SqlDbType.DateTime) { Value = FFC.A1A2EFFC_FeedingDate },
                            new SqlParameter("@ConfirmationTiming", SqlDbType.NVarChar, 50) { Value = FFC.A1A2EFFC_ConfirmationTiming },
                            new SqlParameter("@Feeder", SqlDbType.NVarChar, 50) { Value = FFC.A1A2EFFC_Feeder },
                            new SqlParameter("@FeedingQuantity", SqlDbType.Int) { Value = FFC.A1A2EFFC_FeedingQuantity },
                            new SqlParameter("@ProductModel", SqlDbType.NVarChar, 50) { Value = FFC.A1A2EFFC_ProductModel },
                            new SqlParameter("@LineBody", SqlDbType.NVarChar, 50) { Value = FFC.A1A2EFFC_LineBody },
                            new SqlParameter("@PhotoUpload", SqlDbType.NVarChar, 500) { Value = (object)FFC.A1A2EFFC_PhotoUpload ?? DBNull.Value },
                            new SqlParameter("@CreateCreator", SqlDbType.NVarChar, 50) { Value = FFC.A1A2EFFC_CreateCreator },
                            new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now },
                            new SqlParameter("@FormType", SqlDbType.NVarChar, 50) { Value = FFC.A1A2EFFC_FormType }
                        };

                        using (var command = new SqlCommand(query, connection, transaction))
                        {
                            command.Parameters.AddRange(parameters);
                            var result = await command.ExecuteScalarAsync();
                            if (result != null)
                            {
                                transaction.Commit();
                                FFC.A1A2EFFC_ID = Convert.ToInt32(result);
                                Logger.Info("新增成功");
                                RO.Status = HttpStatusCode.BadRequest;
                                RO.Message = "OK";
                                RO.Data = null;
                                return Request.CreateResponse(HttpStatusCode.OK, RO);
                            }
                            else
                            {
                                transaction.Rollback();
                                Logger.Error("插入记录失败");
                                RO.Status = HttpStatusCode.InternalServerError;
                                RO.Message = "A1A2EmployeeFrameFeedingConfirmation:Error";
                                RO.Data = null;
                                return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Logger.Warn("新增失败: " + ex.Message);
                        RO.Status = HttpStatusCode.InternalServerError;
                        RO.Message = "A1A2EmployeeFrameFeedingConfirmation:Error" + ex.Message;
                        RO.Data = null;
                        return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                    }
                }
            }
        }

    }
}